/**
 * 绑定人员
 * 1. 绑定
 * 2. 添加绑定记录
 * @param defaultParams
 * @param event
 */
function main(defaultParams, event) {
    debugger;
    var self = this; // 重置作用域
    var tableComp = self.$$element("table_8u0dfk"); // 获取表格元素
    let pageRenderElement = self.$$root().$parent.$parent.$parent;
    let vehicleIdValue = pageRenderElement.state.extraParams.id;
    let humanIdValue = defaultParams.id.value;

    self.$$confirm({
        content: "确定绑定？",
        type: "info",  // danger info warning
        headerTitle: "提示", // 确认框头部标题 提示、警告、告警
        showOkBtn: true, // 是否展示确认按钮
        showCancelBtn: true, // 是否展示取消按钮
        cancelText: "取消", // 取消按钮 展示文字
        okText: "确定",  // 确认按钮 展示文字
        onOk: () => {
            console.log(event);
            tableComp.loading = true; // 使表格置于loading状态
            let vehicleHumanData = {
                vehicleHumanBind: '',
                vehicleId: vehicleIdValue,
                vehicleLicenseNumber: '',
                humanId: humanIdValue,
                humanName: ''
            }
            // 调用api的删除方法，第一个参数为待删除的实体id，第二个参数为删除条件、指定实体中的列名和列的值
            api.EXEC_FORMULA("INSERT('t_acd9ea5806fe4ffdb76ee1a1ecb05666',['human_id','" + defaultParams.id.value + "','vehicle_info_id','" + pageRenderElement.state.extraParams.id + "'])")
                .then((res) => {
                    // then() 为调用成功的回调
                    console.log("res", res);
                    insertLawVehicleHumanBindRecord(vehicleHumanData)
                    tableComp.loading = false; // 关闭表格加载状态
                    self.$message.success("操作成功"); // 提示成功信息
                })
                .catch(err => {
                    tableComp.loading = false; // 关闭表格加载状态
                    self.$message.error(err.message); // 显示错误提示信息
                });
        },
        onCancel: () => {
            return; // 取消操作，无需额外逻辑
        }
    });
}


/**
 * 绑定
 * law_vehicle_human_bind_record
 *
 */
function insertLawVehicleHumanBindRecord(vehicleHumanData) {

    const insertFields = [
        ['vehicle_info_id', vehicleHumanData.vehicleId],
        ['vehicle_license_number', `'${vehicleHumanData.vehicleLicenseNumber}'`],
        ['human_id', vehicleHumanData.humanId],
        ['human_name', `'${vehicleHumanData.humanName}'`],
        ['operator_human_id', 'USER_ID()'],
        ['operator_human_name', 'USER_NAME()'],
        ['operator_type_id', `'1'`],
        ['operator_type_name', `'绑定'`],
        ['operator_time', 'NOW()']
    ];

    const formattedFields = insertFields.map(([key, value]) => {
        return `'${key}',${value}`;
    });

    const law_vehicle_human_bind_record = "ds_tbl:t_cac31c58737b421c95fa4070f0e2d8c8";
    const insertSql = `INSERT('${law_vehicle_human_bind_record}', [${formattedFields.join(',')}])`;

    console.log(insertSql);

    api.EXEC_FORMULA(insertSql).then(res => {
        console.log(res)
    })
}
